package offer;

import java.util.Deque;
import java.util.Stack;

/**
 * 剑指 Offer 31. 栈的压入、弹出序列
 */
public class ValidateStackSequences {
    Stack<Integer> stack = new Stack<>();

    public static void main(String[] args) {
        int[] pushed = {1, 2, 3, 4, 5}, popped = {4, 3, 5, 1, 2};
        boolean b = new ValidateStackSequences().validateStackSequences(pushed, popped);
        System.out.println(b);
    }

    public boolean validateStackSequences(int[] pushed, int[] popped) {
        int j = 0;
        for (int i = 0; i < pushed.length; i++) {
            stack.push(pushed[i]);
            while (!stack.isEmpty() && stack.peek() == popped[j]) {
                stack.pop();
                j++;
            }
        }
        return stack.empty();
    }
}
